package org.oproject.rmi.hessian.core.service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.oproject.rmi.hessian.core.job.HessianJob;
import org.oproject.rmi.hessian.core.logger.LogFactory;
import org.oproject.rmi.hessian.core.logger.Logger;
import org.oproject.rmi.hessian.util.BeanHolder;

public abstract class ServiceManager
{	
	private static Logger log = LogFactory.getLogger(ServiceManager.class);
	/**
	 * 保存提供的所有服务接口，该服务接口可以通过从配置文件中加载，也可以从数据库中加载
	 */
	private static Map<String, ServiceInfo> services = Collections.synchronizedMap(new HashMap<String, ServiceInfo>());
	
	/**
	 * 保存系统中，需要使用单例执行的服务对象
	 */
	private static Map<String, Object> singleService = Collections.synchronizedMap(new HashMap<String, Object>());

	/**
	 * 构造函数
	 */
	public ServiceManager(){}
		
	/**
	 * 注册一个服务接口
	 * @param serviceName
	 * @param service
	 */
	public static void regiestService(ServiceInfo serviceInfo)
	{
		if(null != serviceInfo)
		{
			log.info("<-- 加载服务接口[" + serviceInfo.getName() + "] -->");
			services.put(serviceInfo.getName(), serviceInfo);
		}
		else
		{
			throw new RuntimeException("serviceInfo can't been null...");
		}
	}
	
	/**
	 * 注册多个服务接口
	 * @param serviceName
	 * @param service
	 */
	public static void regiestService(Set<ServiceInfo> serviceInfos)
	{
		for(ServiceInfo serviceInfo : serviceInfos)
		{
			regiestService(serviceInfo);
		}
	}
	
	/**
	 * 删除某个服务
	 * @param serviceName
	 */
	public static void removeService(String serviceName)
	{
		services.remove(serviceName);
	}
	
	/**
	 * 删除多个服务
	 * @param serviceName
	 */
	public static void removeService(List<String> serviceNames)
	{
		if(null != serviceNames)
		{
			for(String serviceName : serviceNames)
			{
				services.remove(serviceName);
			}
		}
		else
		{
			throw new RuntimeException("serviceNames can't been null...");
		}
	}
	
	/**
	 * 执行服务
	 * @param serviceName  服务名称
	 * @param args         执行服务需要的参数
	 * @return
	 */
	public abstract Object invokeService(HessianJob hessianJob);
	
	/**
	 * 根据服务名称，获取服务基础信息
	 * @param serviceName
	 * @return
	 */
	protected ServiceInfo getServiceInfo(HessianJob hessianJob)
	{
		ServiceInfo serviceInfo = services.get(hessianJob.getServiceName());
		if(null == serviceInfo)
		{
			throw new RuntimeException("service can't found...");
		}
		else
		{
			return serviceInfo;
		}
	}
	
	/**
	 * 根据服务名称，获取提供服务的实例
	 * @param serviceName
	 * @return
	 */
	protected Object getService(ServiceInfo serviceInfo)
	{
		if(null == serviceInfo)
		{
			throw new RuntimeException("serviceInfo can't been null...");
		}
		else
		{
			switch (serviceInfo.getServiceType())
			{
				case NORMAL:
					/*if(serviceInfo.isSingle())
					{
						Object obj = singleService.get(serviceInfo.getName());
						
						if(null == obj)
						{
							obj = null;
							
							singleService.put(serviceInfo.getName(), obj);
						}
						
						return obj;
					}
					else
					{
						
						 * TODO 非单例的生成方法
						 
						throw new RuntimeException("can't support type...");
					}*/
					throw new RuntimeException("sorry,this version only support spring bean...");
				case SPRING:
					return BeanHolder.getBean(serviceInfo.getName());
				default:
					throw new RuntimeException("sorry,this version only support spring bean...");
			}
		}
	}
}
